package com.liezh.client;

import com.liezh.SessionUtil;
import com.liezh.service.SslThriftService;
import com.liezh.service.User;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liezh
 * @date 2021-9-3
 */
public class Client {

    public static void main(String[] args) {
//        bar();
        foo();
    }

    private static void bar() {
        TTransport transport = null;
        try {
            TSSLTransportFactory.TSSLTransportParameters params
                    = new TSSLTransportFactory.TSSLTransportParameters();

            String truststoreFilename = "src/truststore";
            String truststorePassword = "thrift";
            params.setTrustStore(truststoreFilename, truststorePassword, "SunX509", "JKS");
            transport = TSSLTransportFactory.getClientSocket("localhost", 9001, 0, params);
            TFramedTransport tFramedTransport = new TFramedTransport(transport);
            TProtocol protocol = new TBinaryProtocol(tFramedTransport);

            SslThriftService.Client client = new SslThriftService.Client(protocol);
            User bar = new User(12, "liezh", "123456");
            String msg = client.login(bar);
            System.out.println(msg);
        } catch (TException x) {
            x.printStackTrace();
        } finally {
            if (transport != null) {
                transport.close();
            }
        }
    }

    private static void foo() {
        TSocket transport = null;
        try {
            TSSLTransportFactory.TSSLTransportParameters params
                    = new TSSLTransportFactory.TSSLTransportParameters();

            String truststoreFilename = "src/test.keystore";
            String truststorePassword = "spIT2021#";
            params.setTrustStore(truststoreFilename,
                    truststorePassword,
                    "SunX509",
                    "JKS");
            transport = TSSLTransportFactory.getClientSocket("localhost", 9001, 0, params);
//            TFramedTransport tFramedTransport = new TFramedTransport(transport);
            TProtocol protocol = new THeaderClientProtocol(transport);

            SslThriftService.Client client = new SslThriftService.Client(protocol);
            User bar = new User(12, "liezh", "123456");
            String msg = client.login(bar);
            System.out.println(msg);
            Map<String, String> session = new HashMap<>();
            session.put("id", "12");
            session.put("username", "liezh");
            SessionUtil.setSession(session);
            final String liezh = client.queryUserInfo("liezh");
            System.out.println(liezh);
        } catch (TException x) {
            x.printStackTrace();
        } finally {
            if (transport != null) {
                transport.close();
            }
        }
    }
}
